$(function(){
	$.post("./acounts.json", function(acounts) {
		if(acounts){
			$("#acountChoose").datagrid("loadData",acounts);
		}
	});
	$("input[name='beginDateType']").change(function() {
		refreshProfit();
	});
});

function refresh(isRefresh){
	var selected = $("#acountChoose").datagrid("getSelections");
	if(selected.length<2){
		$.messager.show({
			title:'错误消息',
			timeout:1000,
			msg:'最少选择两个',
			showType:'fade',
            style:{
                right:'',
                bottom:''
            }
		});
		return;
	}
	var acountIds = "";
	for(var i=0;i<selected.length;i++){
		acountIds += ","+selected[i].acountId;
	}
	acountIds=acountIds.substr(1);		
	$.ajax("./cmpreport.json",{
		data : {
			acountIds : acountIds,
			isRefresh : isRefresh
		},
		mask:true,
		success:function(data) {
			if(data.modelconfig){
				$("#acountChoose").datagrid("getPanel").panel("collapse");
				loadModelConfig(data.modelconfig, data.modelParams);
				$("#risk").datagrid("loadData",data.risk);
				loadProfitChart(data.profit);
			}
		}
	});
}

function refreshProfit(){
	var selected = $("#acountChoose").datagrid("getSelections");
	if(selected.length<2){
		$.messager.show({
			title:'错误消息',
			timeout:1000,
			msg:'最少选择两个',
			showType:'fade',
            style:{
                right:'',
                bottom:''
            }
		});
		return;
	}
	var acountIds = "";
	for(var i=0;i<selected.length;i++){
		acountIds += ","+selected[i].acountId;
	}
	acountIds=acountIds.substr(1);
	
	var data = $("#requestForm").form("getData");
	data.acountIds = acountIds;
	$.ajax("./cmpprofit.json",{
		mask:true,
		data:data,
		success:function(data) {
			if(data.profit){
				loadProfitChart(data.profit);
			}
		}
	});
}

function changeBeginDate() {
	$("input[name='beginDateType']:checked").removeAttr("checked");
	refreshProfit();
}

function loadProfitChart(data){
	// 基于准备好的dom，初始化echarts实例
	var myChart = echarts.init(document.getElementById('report1'));
	var lines = data.lineData;
	var legend = [];
	var series = [];
	for ( var key in lines) {
		legend.push(key);
		series.push({
			name : key,
			type : 'line',
			data : lines[key]
		});
	}
	
	// 指定图表的配置项和数据
	var option = {
		title: {
			text: '收益报告'
		},
		tooltip : {
	        trigger: 'axis',
	        axisPointer: {
	            type: 'cross' 
	        },
			formatter : function(params) {
				var res = params[0].name;
				for (var i = 0, l = params.length; i < l; i++) {
					var a = '<span style="display:inline-block;margin-right:5px;border-radius:10px;width:9px;height:9px;background-color:' + params[i].color + '"></span>';
					res += '<br/>' + a + params[i].seriesName + ' : ' + params[i].value.toFixed(2) + "%";
				}
				return res;
			}
	    },
		legend: {
			data:legend
		},
		dataZoom:{
			show: true,
			realtime: true,
			start: 0,
			end: 100
		},
		xAxis: {
		 	type: "category",
		    data: data.timeLine
		},
	    yAxis: [{
			name: '收益%',
			type: 'value',
	            }
	        ],
		series:series
	};
	myChart.setOption(option);
}

function loadModelConfig(modelConfig, modelParams){
	var colunms = $("#modelconfig").datagrid("options").columns;
	var newColunms = colunms[0].slice(0, 7);
	for(var i=0;i<modelParams.length;i++){
		var modelParam = modelParams[i];
		var column = {field:modelParam.paramCode,title:modelParam.paramName,width:70};
		newColunms.push(column);
	}
	$("#modelconfig").datagrid({      
	    columns:[newColunms]
	});
	$("#modelconfig").datagrid("loadData",modelConfig);
}